<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;

class LoginController extends Controller
{
    /**
     * [store 登录]
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function store(Request $request)
    {
        $credentials = $this->validate($request,[
            'email' => 'required|email|max:255',
            'password' => 'required'
        ]);

        if(Auth::attempt($credentials,$request->has('remember'))){

            //登录成功后的相关操作
            return redirect()->intended(route('products',[Auth::user()]));
             // 合约周期
            $contract = Auth::user()->contracts()->orderBy('created_at', 'desc')->first();
            // dd($contract);
            // if( ! $contract) {
            //     Auth::logout(); return redirect('login');
            // }
            // 合约创建时间
            $created_at = Auth::user()->contracts()->orderBy('created_at', 'desc')->first()->created_at;
            // 合约结束时间
            $endtime = (int)bcadd(strtotime($created_at),bcmul($contract->cycle,86400));

            // if($endtime > time()) {
            //     //登录成功后的相关操作
            //     return redirect()->intended(route('products',[Auth::user()]));
            // } else {
            //     Auth::logout();
            //     return redirect('login');
            // }
        }else{
            //登录失败后的相关操作
            return redirect()->back();
        }
        return;

    }


    /**
     * [destroy 退出登录]
     * @return [type] [description]
     */
    public function destroy()
    {
        Auth::logout();
        return redirect('login');
    }


}
